using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.Interfaces;
using FiltronASP.ViewModels.CMS;
using FiltronASP.Consts;
using System.Text.RegularExpressions;
using FiltronASP.Helpers.AjaxModel;
using FiltronASP.Helpers.LastSearches;
using FiltronASP.Helpers.ReplacementsHelper;
using System.Net.Mail;
using FiltronASP.Logger;
using FiltronASP.Helpers.Translations;
using System.Configuration;

namespace FiltronASP.Controllers
{
    public class AjaxController : Controller
    {
        [OutputCache(Duration = 1800, VaryByParam = "masterItemId;languageName")]
        public ActionResult ModelMasterDetailAjaxFiltron(int? masterItemId, string languageName)
        {
            try
            {
                if (masterItemId.HasValue)
                {
                    return Content(ModelHelperFiltron.GetModelComboString(masterItemId.Value, languageName));                    
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("---|-1;");
        }

        [OutputCache(Duration = 1800, VaryByParam = "market;languageName")]
        public ActionResult GetAjaxVehicles(string market, string languageName)
        {
            try
            {
                return Content(ModelHelper.GetVehiclesComboString(market, languageName));
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content(translationHelper.getTranslation("_mainSearchSelectVehicleType", languageName) + "|-1;");
        }

        public ActionResult addLastSearchesAjax(int? engineId, int? makeId, int? modelId, int? filterId, string filterProd, string languageName)
        {
            try
            {
                if ((!engineId.HasValue) || (!makeId.HasValue) || (!modelId.HasValue) || (!filterId.HasValue))
                {
                    return Content("Err");
                }   //if
                LastSearchesHelper.makeLastSearch(makeId.Value, modelId.Value, engineId.Value, filterId.Value, filterProd, languageName);
                return Content("OK");
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("Err");
        }

        public ActionResult AutoFillFiltron(string query)
        {
            string givenString = "";
            try
            {
                if (!String.IsNullOrEmpty(query))
                {
                    givenString += query;
                    givenString = givenString.Replace("+", "");
                    givenString = givenString.Replace(" ", "");
                    return Content(AutoFillHelper.FillInputByGivenStringFiltron(Convert.ToString(givenString)));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("{ query:'" + givenString + "',suggestions:['no_filters']}");
        }

        public ActionResult AutoFillWix(string query)
        {
            string givenString = "";
            try
            {
                if (!String.IsNullOrEmpty(query))
                {
                    givenString += query;
                    givenString = givenString.Replace("+", "");
                    givenString = givenString.Replace(" ", "");
                    return Content(AutoFillReplacementsHelper.FillInputByGivenStringReplacement(Convert.ToString(givenString)));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("{ query:'" + givenString + "',suggestions:['no_filters']}");
        }

        public ActionResult AutoFillReplacements(string query)
        {
            string givenString = "";
            try
            {
                if (!String.IsNullOrEmpty(query))
                {
                    givenString += query;
                    givenString = givenString.Replace("+", "");
                    givenString = givenString.Replace(" ", "");
                    return Content(AutoFillReplacementsHelper.FillInputByGivenString(Convert.ToString(givenString)));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("{ query:'" + givenString + "',suggestions:['no_filters']}");
        }

        public ActionResult AutoFillReplacementsRep(string query)
        {
            string givenString = "";
            try
            {
                if (!String.IsNullOrEmpty(query))
                {
                    givenString += query;
                    givenString = givenString.Replace("+", "");
                    givenString = givenString.Replace(" ", "");
                    return Content(AutoFillReplacementsHelper.FillInputByGivenStringReplacement(Convert.ToString(givenString)));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("{ query:'" + givenString + "',suggestions:['no_filters']}");
        }

        [OutputCache(Duration = 1800, VaryByParam = "masterItemId;languageName")]
        public ActionResult BrandMasterDetailAjax(int? masterItemId, string languageName)
        {
            try
            {
                if (masterItemId.HasValue)
                {
                    return Content(ModelHelper.getBrandDropDownByVehicleId(masterItemId.Value, languageName));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("---,-1");
        }

        [OutputCache(Duration = 1800, VaryByParam = "masterItemId;languageName")]
        public ActionResult BrandMasterDetailAjaxFiltron(string masterItemId, string languageName)
        {
            try
            {
                if (!String.IsNullOrEmpty(masterItemId))
                {
                    return Content(ModelHelperFiltron.getBrandDropDownByVehicleId(masterItemId, languageName));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("---,-1");
        }

        [OutputCache(Duration = 1800, VaryByParam = "masterItemId;languageName")]
        public ActionResult EngineMasterDetailAjax(int? masterItemId, string languageName)
        {
            try
            {
                if (masterItemId.HasValue)
                {                    
                    return Content(ModelHelper.GetEngineDivString(masterItemId.Value, languageName));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content(ModelHelper.GetEngineDivString());
        }

        [OutputCache(Duration = 1800, VaryByParam = "masterItemId;languageName")]
        public ActionResult EngineMasterDetailAjaxFiltron(int? masterItemId, string languageName)
        {
            try
            {
                if (masterItemId.HasValue)
                {
                    return Content(ModelHelperFiltron.GetEngineDivString(masterItemId.Value, languageName));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content(ModelHelperFiltron.GetEngineDivString());
        }

        [OutputCache(Duration = 1800, VaryByParam = "filterId;languageName")]
        public ActionResult FiltersSingleAjax(int? filterId, string languageName)
        {
            try
            {
                if (filterId.HasValue)
                {
                    return Content(ModelHelper.generateSingleFilter(filterId.Value, languageName));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content(ModelHelper.GetEngineDivString());
        }

        [OutputCache(Duration = 1800, VaryByParam = "filterId;languageName;isHdTruck")]
        public ActionResult FiltersSingleAjaxFiltron(int? filterId, string languageName, bool? isHdTruck)
        {
            try
            {
                if (filterId.HasValue)
                {
                    if (!isHdTruck.HasValue)
                    {
                        isHdTruck = false;
                    }   //if
                    return Content(ModelHelperFiltron.generateSingleFilter(filterId.Value, languageName, isHdTruck.Value));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content(ModelHelperFiltron.GetEngineDivString());
        }

        [OutputCache(Duration = 1800, VaryByParam = "engineId;modelId;languageName")]
        public ActionResult FiltersTableAjax(int? engineId, int? modelId, string languageName)
        {
            try
            {
                if (engineId.HasValue)
                {
                    if (!modelId.HasValue)
                    {
                        modelId = 0;
                    }   //if
                    return Content(ModelHelper.GetFiltersByEngineAndModel(engineId.Value, modelId.Value, languageName));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("Error");
        }

        [OutputCache(Duration = 1800, VaryByParam = "engineId;modelId;languageName")]
        public ActionResult FiltersTableAjaxFiltron(int? engineId, int? modelId, string languageName)
        {
            try
            {
                if (engineId.HasValue)
                {
                    if (!modelId.HasValue)
                    {
                        modelId = 0;
                    }   //if
                    return Content(ModelHelperFiltron.GetFiltersByEngineAndModel(engineId.Value, modelId.Value, languageName));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("Error");
        }

        public ActionResult getSingleMonth(string monthName, int? languageId, string currentMonth, int? currentLanguage, string year)
        {
            try
            {
                if (!String.IsNullOrEmpty(monthName))
                {
                    if (!languageId.HasValue)
                    {
                        languageId = 0;
                    }   //if
                    if (!currentLanguage.HasValue)
                    {
                        currentLanguage = 0;
                    }   //if
                    return Content(implementationsHelpers.getSingleMonth(monthName, currentLanguage.Value, year));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("Err");
        }

        public ActionResult LastSearchesList(int? pagination, int? languageId)
        {
            try
            {
                if (!pagination.HasValue)
                {
                    pagination = 1;
                }   //if
                if (!languageId.HasValue)
                {
                    languageId = 0;
                }   //if
                return Content(LastSearchesListHelper.generatePaginatedLastSearchesList(pagination.Value, languageId.Value));
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("Err");
        }

        [OutputCache(Duration = 1800, VaryByParam = "masterItemId;languageName")]
        public ActionResult ModelMasterDetailAjax(int? masterItemId, string languageName)
        {
            try
            {
                if (masterItemId.HasValue)
                {
                                        
                    return Content(ModelHelper.GetModelComboString(masterItemId.Value, languageName));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("---|-1");
        }

        public ActionResult MultisearchReplacementFilterList(string value, string languageName)
        {
            try
            {
                if (!String.IsNullOrEmpty(value))
                {
                    return Content(MultisearchReplacementList.generateReplacementList(value, languageName));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content(String.Empty);
        }

        [OutputCache(Duration = 1800, VaryByParam = "filterId;producer;languageName")]
        public ActionResult ReplacementFilterDescription(int? filterId, string producer, string languageName)
        {
            try
            {
                if (!filterId.HasValue)
                {
                    filterId = 0;
                }   //if
                if (!(filterId.Equals(0)))
                {
                    return Content(ModelHelper.generateReplacementDescription(filterId.Value, producer, languageName));
                }   //if
                return Content("FilterId error.");
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("Error.");
        }

        [OutputCache(Duration = 1800, VaryByParam = "filterId;producer;languageName")]
        public ActionResult ReplacementFilterInfo(int? filterId, string producer, string languageName)
        {
            try
            {
                if (!filterId.HasValue)
                {
                    filterId = 0;
                }   //if
                if (!(filterId.Equals(0)))
                {
                    return Content(ModelHelper.generateReplacementInfo(filterId.Value, producer, languageName));
                }   //if
                return Content("FilterId error.");
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("Error.");
        }

        [OutputCache(Duration = 1800, VaryByParam = "year;languageName")]
        public ActionResult MonthMasterDetailAjax(string year, string languageName)
        {
            try
            {
                if (true)
                {
                    return Content(ModelHelper.GetMonthComboString(year, languageName));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("---|-1;");
        }

        [OutputCache(Duration = 1800, VaryByParam = "month;languageName;languageId")]
        public ActionResult ImplementationsMasterDetailAjax(string month, string languageName, int? languageId, string year)
        {
            try
            {
                if (month!=null)
                {
                    int languageIdInt = 7;  //en
                    if (languageId.HasValue)
                    {
                        languageIdInt = languageId.Value;
                    }   //if
                    return Content(implementationsHelpers.getSingleMonth(month, languageIdInt, year));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content(ModelHelperFiltron.GetEngineDivString());
        }

        public ActionResult ReplacementFilterList(string value, string languageName)
        {
            try
            {
                if (!String.IsNullOrEmpty(value))
                {
                    return Content(ReplacementList.generateReplacementList(value, languageName));
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content(String.Empty);
        }

        public ActionResult SearchedOnMain(string filter_number, string manufacturer, string languageName)
        {
            try
            {
                if (!String.IsNullOrEmpty(filter_number))
                {
                    return Content(ModelHelperFiltron.generateSearchedFilterDescription(manufacturer, filter_number, languageName));
                }   //if
                return Content("FilterId error.");
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content(String.Empty);
        }

        public ActionResult SizeApplDetailAjax(decimal? dimA, decimal? dimB, decimal? dimC, decimal? dimD, decimal? dimE,
                                decimal? dimF, decimal? dimG, decimal? dimH, int? dimAdiff, int? dimBdiff, int? dimCdiff,
                                int? dimDdiff, int? dimEdiff, int? dimFdiff, int? dimGdiff, int? dimHdiff, string selectedType,
                                string selectedShape, string languageName, int start, int offset)
        {


            try
            {
                decimal ZERO = 0;
                int intZERO = 0;
                if (!dimA.HasValue)
                {
                    dimA = ZERO;
                }   //if
                if (!dimB.HasValue)
                {
                    dimB = ZERO;
                }   //if
                if (!dimC.HasValue)
                {
                    dimC = ZERO;
                }   //if
                if (!dimD.HasValue)
                {
                    dimD = ZERO;
                }   //if
                if (!dimE.HasValue)
                {
                    dimE = ZERO;
                }   //if
                if (!dimF.HasValue)
                {
                    dimF = ZERO;
                }   //if
                if (!dimG.HasValue)
                {
                    dimG = ZERO;
                }   //if
                if (!dimH.HasValue)
                {
                    dimH = ZERO;
                }   //if

                if (!dimAdiff.HasValue)
                {
                    dimAdiff = intZERO;
                }   //if
                if (!dimBdiff.HasValue)
                {
                    dimBdiff = intZERO;
                }   //if
                if (!dimCdiff.HasValue)
                {
                    dimCdiff = intZERO;
                }   //if
                if (!dimDdiff.HasValue)
                {
                    dimDdiff = intZERO;
                }   //if
                if (!dimEdiff.HasValue)
                {
                    dimEdiff = intZERO;
                }   //if
                if (!dimFdiff.HasValue)
                {
                    dimFdiff = intZERO;
                }   //if
                if (!dimGdiff.HasValue)
                {
                    dimGdiff = intZERO;
                }   //if
                if (!dimHdiff.HasValue)
                {
                    dimHdiff = intZERO;
                }   //if
                return Content(sizesHelperFiltron.generateFiltersBySizesPaginated(dimA.Value, dimB.Value, dimC.Value, dimD.Value, dimE.Value,
                    dimF.Value, dimG.Value, dimH.Value, dimAdiff.Value, dimBdiff.Value, dimCdiff.Value, dimDdiff.Value,
                    dimEdiff.Value, dimFdiff.Value, dimGdiff.Value, dimHdiff.Value, selectedType, selectedShape, languageName, start, offset));
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("No filters.");
        }

        public ActionResult SizeApplDetailAjaxPag(decimal? dimA, decimal? dimB, decimal? dimC, decimal? dimD, decimal? dimE,
                                decimal? dimF, decimal? dimG, decimal? dimH, int? dimAdiff, int? dimBdiff, int? dimCdiff,
                                int? dimDdiff, int? dimEdiff, int? dimFdiff, int? dimGdiff, int? dimHdiff, string selectedType,
                                string selectedShape, string languageName, int start, int offset)
        {


            try
            {
                decimal ZERO = 0;
                int intZERO = 0;
                if (!dimA.HasValue)
                {
                    dimA = ZERO;
                }   //if
                if (!dimB.HasValue)
                {
                    dimB = ZERO;
                }   //if
                if (!dimC.HasValue)
                {
                    dimC = ZERO;
                }   //if
                if (!dimD.HasValue)
                {
                    dimD = ZERO;
                }   //if
                if (!dimE.HasValue)
                {
                    dimE = ZERO;
                }   //if
                if (!dimF.HasValue)
                {
                    dimF = ZERO;
                }   //if
                if (!dimG.HasValue)
                {
                    dimG = ZERO;
                }   //if
                if (!dimH.HasValue)
                {
                    dimH = ZERO;
                }   //if

                if (!dimAdiff.HasValue)
                {
                    dimAdiff = intZERO;
                }   //if
                if (!dimBdiff.HasValue)
                {
                    dimBdiff = intZERO;
                }   //if
                if (!dimCdiff.HasValue)
                {
                    dimCdiff = intZERO;
                }   //if
                if (!dimDdiff.HasValue)
                {
                    dimDdiff = intZERO;
                }   //if
                if (!dimEdiff.HasValue)
                {
                    dimEdiff = intZERO;
                }   //if
                if (!dimFdiff.HasValue)
                {
                    dimFdiff = intZERO;
                }   //if
                if (!dimGdiff.HasValue)
                {
                    dimGdiff = intZERO;
                }   //if
                if (!dimHdiff.HasValue)
                {
                    dimHdiff = intZERO;
                }   //if
                return Content(sizesHelperFiltron.generateFiltersBySizesPaginated(dimA.Value, dimB.Value, dimC.Value, dimD.Value, dimE.Value,
                    dimF.Value, dimG.Value, dimH.Value, dimAdiff.Value, dimBdiff.Value, dimCdiff.Value, dimDdiff.Value,
                    dimEdiff.Value, dimFdiff.Value, dimGdiff.Value, dimHdiff.Value, selectedType, selectedShape, languageName, start, offset));
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
            return Content("No filters.");
        }

        public ActionResult sendMail(string cntName, string cntEmail,
                                string cntAddress, string cntCity, string contactZipCode,
                                string cntCountry, string cntPhone, string cntQuestion)
        {
            string cntSubject = "Wiadomość z formularza kontaktowego wixeurope.com";
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("formularz@b2net.pl");
            msg.To.Add(new MailAddress("wixeurope@wixeurope.com"));
            //msg.To.Add(new MailAddress("marek.fechner@b2net.pl"));
            string cntZipCode = contactZipCode;
            string msgBody = "Imię i nazwisko: " + cntName + "\r\n" + "Email: " + cntEmail + "\r\n" + "Adres: " + cntAddress + "\r\n" + "Miasto: " + cntCity + "\r\n" + "Kod pocztowy: " + cntZipCode + "\r\n" + "Kraj: " + cntCountry + "\r\n" + "Telefon: " + cntPhone + "\r\n" + "Pytanie: " + cntQuestion;
            msg.Subject = cntSubject;
            msg.Body = msgBody;
            try
            {
                SmtpClient SMTP = new SmtpClient();
                SMTP.EnableSsl = true;
                SMTP.Send(msg);
                return Content("OK");
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return Content("Error");
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult sendFilter(string email, string fname, string dimensions, string applications, string other, string from)
        {            
            string cntSubject = "Filter from wixeurope.com";
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("formularz@b2net.pl");
            //msg.To.Add(new MailAddress("wixeurope@affiniagroup.com"));
          
            msg.To.Add(new MailAddress(email));
            string msgBody = "<h4>Filter name: " + fname + "</h4><br/><br/> " + dimensions + "<br/><br/> " + applications;
            msgBody += "<br/><br/> " + other;
            msgBody += "<br/><br/><br/> Recommended by " + from;
            msg.Subject = cntSubject;
            msg.Body = msgBody;
            msg.IsBodyHtml = true;
            try
            {
                SmtpClient SMTP = new SmtpClient();
                SMTP.EnableSsl = true;
                SMTP.Send(msg);
                return Content("OK");
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return Content("Error");
        }

        
    }
}
